#include "StdAfx.h"
#include "PlayerBodyController.h"
#include "PlayerBodyControlLogic.h"
#include "EntityControl.h"
#include "PhysicsEntity.h"
#include "PlayerControlElement.h"
#include "ControlContext.h"
#include "ControlStatus.h"



PlayerBodyController::PlayerBodyController(PlayerBodyControlLogic* playerBodyControlLogic)
	:playerBodyControlLogic(playerBodyControlLogic)
{
	position = Ogre::Vector3(0.0,0.0,0.0);
}


PlayerBodyController::~PlayerBodyController(void)
{
	delete playerBodyControlLogic;
}

bool PlayerBodyController::isSpawningNeeded()
{
	return false;
}
void PlayerBodyController::apply(EntityControl* entity, const ControlContext& context)
{
	PhysicsEntity* bulletActor = entity->getPhysicsActor();

	

	// Move
	Move::IMoveManager* move = context.controlStatus.getController();


	Move::Quaternion palyer_oriMove = move->getOrientation(1);
	Ogre::Quaternion player_ori = Ogre::Quaternion(palyer_oriMove.w, palyer_oriMove.x, palyer_oriMove.y,palyer_oriMove.z);
	
	
	NxActor* actor = entity->getPhysicsActor()->getNxActor();
	Ogre::SceneNode* ogreNode = entity->getPhysicsActor()->getOgreNode();
	
	Ogre::Vector3 ahead = ogreNode->getOrientation() * Ogre::Vector3::UNIT_Z;
	ahead.y=0;
	Ogre::Vector3 ahead_rotated = Ogre::Vector3(-1.0*ahead.z,0,ahead.x);
	position += ahead*player_ori.getPitch().valueRadians()*0.8;
	position += ahead_rotated*player_ori.getYaw().valueRadians()*0.8;

	Ogre::Vector3 pos = position;

	actor->setLinearVelocity(NxVec3(0,0,0));
	actor->setAngularVelocity(NxVec3(0,0,0));


	//ogreNode->setOrientation(player_ori);
	ogreNode->setPosition(pos);


	Ogre::SceneNode* tempNode = ogreNode;
	pos = tempNode->_getDerivedPosition();
	Ogre::Quaternion orient = tempNode->_getDerivedOrientation();
	actor->moveGlobalPosition(NxVec3(pos.x,pos.y,pos.z));
	//actor->moveGlobalOrientation(NxQuat(NxVec3(orient.x,orient.y,orient.z),orient.w));

}

Ogre::String PlayerBodyController::getType()
{
	return "PlayerBodyController";
}

Ogre::Vector3 PlayerBodyController::getPosition()
{
	return position;
}

